package com.mgm.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.mgm.creative.service.CreativeService;
import com.mgm.exception.CustomException;
import com.mgm.page.DataGrid;
import com.mgm.pojo.Creative;
import com.mgm.result.IMessage;
import com.mgm.result.IResult;

/**
 * 
 * @ClassName: CreativeController 
 * @Description: 素材
 * @author Andy Zhang
 * @date 2018年7月12日 下午5:05:55 
 *
 */
@RestController
@RequestMapping("/creatives")
public class CreativeController {
	
	private static Logger logger = LoggerFactory.getLogger(CreativeController.class);
	
	@Autowired
	private CreativeService creativeService;
	
	/**
	 * 素材列表
	 * @Title: list   
	 * @Description: TODO(...)   
	 * @param: @param pageNum
	 * @param: @param pageSize
	 * @param: @param offerid
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IResult<DataGrid>>      
	 * @throws
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<IResult<DataGrid>> list(
			@RequestParam(value="offerid",required=true) String offerid,
			@RequestParam(value="page", defaultValue="1") Integer pageNum,
			@RequestParam(value="ps", defaultValue="10") Integer pageSize, 
			HttpServletRequest request, HttpServletResponse response) {
		DataGrid dg = creativeService.findAll(offerid, pageNum, pageSize);
		IResult<DataGrid> result = IResult.success(dg);
		return new ResponseEntity<IResult<DataGrid>>(result, HttpStatus.OK);
	}
	
	/**
	 * 创建素材
	 * @Title: createCreative
	 * @Description: TODO(...)   
	 * @param: @param creative
	 * @param: @return      
	 * @return: ResponseEntity<IMessage>      
	 * @throws
	 */
	@RequestMapping(method = RequestMethod.POST) 
	public ResponseEntity<IMessage> createCreative(@RequestBody Creative creative){
		IMessage msg = creativeService.addCreative(creative);
		return new ResponseEntity<IMessage>(msg, HttpStatus.CREATED);
	}
	
	/**
	 * 素材详情
	 * @Title: getCreative   
	 * @Description: TODO(...)   
	 * @param: @param aderId
	 * @param: @param admentId
	 * @param: @param id
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IResult<Creative>>      
	 * @throws
	 */
	@RequestMapping(value="/{uid}", method = RequestMethod.GET)
	public ResponseEntity<IResult<Creative>> getCreative(@PathVariable("uid") String uid, 
			HttpServletRequest request, HttpServletResponse response) {
		Creative creative = creativeService.selectById(uid);
		IResult<Creative> result = IResult.success(creative);
		
		return new ResponseEntity<IResult<Creative>>(result, HttpStatus.OK);
	}
	
	/**
	 * 编辑素材
	 * @Title: updateCreative   
	 * @Description: TODO(...)   
	 * @param: @param creative
	 * @param: @return      
	 * @return: ResponseEntity<IMessage>      
	 * @throws
	 */
	@RequestMapping(method = RequestMethod.PUT) 
	public ResponseEntity<IMessage> updateCreative(@RequestBody Creative creative){
		IMessage msg = creativeService.updateCreative(creative);
		return new ResponseEntity<IMessage>(msg, HttpStatus.CREATED);
	}
	
	/**
	 * 删除素材
	 * @Title: deleteAdvertisement   
	 * @Description: TODO(...)   
	 * @param: @param offerid
	 * @param: @param id
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IMessage>      
	 * @throws
	 */
	@RequestMapping(value = "/{uid}/{offerid}", method = RequestMethod.DELETE) 
	public ResponseEntity<IMessage> deleteAdvertisement(@PathVariable("uid") String uid,
			@PathVariable("offerid") String offerid, 
			HttpServletRequest request, HttpServletResponse response) {
		Creative creative = new Creative();
		creative.setUid(uid);
		creative.setOfferid(offerid);
		IMessage msg = creativeService.deleteCreative(creative);
		return new ResponseEntity<IMessage>(msg, HttpStatus.OK);
	}
	
	/**
	 * 素材开关
	 * @Title: enabled   
	 * @Description: TODO(...)   
	 * @param: @param aderId
	 * @param: @param admentId
	 * @param: @param id
	 * @param: @param map
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IMessage>      
	 * @throws
	 */
	@RequestMapping(value="/{uid}/{offerid}/enabled", method = RequestMethod.PUT)
	public ResponseEntity<IMessage> enabled(@PathVariable("uid") String uid, 
			@PathVariable("offerid") String offerid,
			@RequestBody Map<String, Boolean> map, 
			HttpServletRequest request, HttpServletResponse response) {
		if(map==null || map.get("enabled")==null) {
			logger.error("[Enabled Advertisement] Illegal operation");
			throw new CustomException("非法操作");
		}
		IMessage msg = creativeService.isEnabled(uid, offerid, map.get("enabled"));
		return new ResponseEntity<IMessage>(msg, HttpStatus.OK);
	}

}
